<template>
    <div class="app-container">
        <div class="warp_padding">  
            <div class="head-container">
                <!-- 搜索 -->
                <label id="labelQuery" class="labelQuery">账号: </label>
                <el-input v-model="formDate.userId" clearable placeholder="输入账号搜索" style="width: 140px;" class="filter-item" @keyup.enter.native="toQuery" @change="toQuery()"/>
                

                <span v-if="device!=='mobile'">
                    <label class="labelQuery">姓名: </label>
                    <el-input v-model="formDate.userName" clearable placeholder="输入姓名搜索" style="width: 140px;" class="filter-item" @keyup.enter.native="toQuery" @change="toQuery()"/>

                    <label class="labelQuery">手机: </label>
                    <el-input v-model="formDate.mobile" clearable placeholder="输入手机号搜索" style="width: 160px;" class="filter-item" @keyup.enter.native="toQuery" @change="toQuery()" maxlength="11"/>

                    <label class="labelQuery">状态: </label>
                    <el-select v-model="formDate.status" clearable placeholder="请选择状态搜索" @change="toQuery()" style="width:162px">
                        <el-option
                              v-for="item in condition"
                              :key="item.id"
                              :label="item.name"
                              :value="item.id">
                        </el-option>
                    </el-select>
                </span>

                <el-button class="filter-item" size="mini" type="primary" icon="el-icon-search" @click="toQuery">搜索</el-button>
                <!-- 新增 -->
                <div style="display: inline-block;margin: 0px 2px;">
                  <el-button
                    v-if="checkPermission(['admin','user_all','user_add'])"
                    class="filter-item"
                    size="mini"
                    type="primary"
                    icon="el-icon-plus"
                    @click="add">新增</el-button>
                  <eForm ref="form" :is-add="isAdd" :roles="roles"/>
                </div>

            </div>


            <!--列表-->
            <el-table :data="lists" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;" max-height="650">
                <el-table-column type="selection" width="30"></el-table-column>
                
                <el-table-column type="index" width="45"></el-table-column>
                
                <el-table-column prop="userId" label="账号"   :show-overflow-tooltip="true" ></el-table-column>            

                <el-table-column prop="userName" label="姓名"   :show-overflow-tooltip="true" ></el-table-column>
               
                <el-table-column prop="mobile" label="手机号" min-width="100" ></el-table-column>

                <el-table-column prop="cardId" label="身份证号" :show-overflow-tooltip="true" ></el-table-column>

                <el-table-column prop="status" label="状态" width="100"  sortable>
                     <template slot-scope="scope">
                        <el-tag :type="scope.row.status ? 'info' : 'success'">{{ scope.row.status ? '禁用' : '启用' }}</el-tag>
                    </template>
                </el-table-column>

                <el-table-column prop="createtime" label="创建日期"  min-width="120" >
                    <template slot-scope="scope">
                      <span>{{scope.row.createtime | formatDate}}</span>
                    </template>
                </el-table-column>


                <el-table-column label="操作" width="150">
                    <template slot-scope="scope">
                        <el-button size="mini" type="success" v-if="checkPermission(['admin','user_all','uesr_edit'])" @click="handleEdit(scope.$index, scope.row.id)">编辑</el-button>
                        <el-button type="danger" size="mini" v-if="checkPermission(['admin','user_all','user_del'])" @click="handleDel(scope.$index, scope.row.id)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>

        <!--工具条-->
        <el-col :span="24" class="toolbar">
            <el-button type="danger" size="mini" @click="batchRemove" :disabled="this.sels.length===0" class="remove">批量删除</el-button>

            <el-pagination background  @size-change="handleSizeChange"  @current-change="handleCurrentChange"  :current-page.sync="formDate.pageNo" :page-count="pageCount" :page-sizes="[10, 20, 30, 50,100]"  layout="total, sizes,prev, pager, next, jumper" :pager-count="pagerCount" :total="total" class="pagination"></el-pagination>
        </el-col>

    </div>
</template>

<script>
import { formatDate } from '@/utils/formatDate.js'
import checkPermission from '@/utils/permission' 
import { parseTime } from '@/utils/index'
import eForm from './module/form'
import { synctea, userList, userDel, getRoles } from '@/api/request'
import { editUser,syngroup } from '@/api/data'
import { mapGetters } from 'vuex'
export default {
    components: { eForm },
    computed: {
        ...mapGetters([
          'device'
        ])
    },
    filters: {
        formatDate(time) {
            let date = new Date(time);
            return formatDate(date, 'yyyy-MM-dd hh:mm');
        }, 
    },
    data() {
        return {
            pageCount: 0,
            total:0,  
            listLoading: false,
            lists: [],
            sels: [],//列表选中列
            pagerCount:11,   
            formDate:{
                pageNo: 1,  
                pageSize:10,
                userType:'0',
                userId:'',
                userName:'',
                mobile:'',
                status:''   
            },
            isAdd:true,
            roles:[],
            condition:[
                {
                    id: '0',
                    name: '启用'
                },
                {
                    id: '1',
                    name: '禁用'
                },
            ]
        }
    },
    mounted:function(){
        this.ready()
        this.init()
    },
    methods: {
        parseTime,
        checkPermission,
        // type(row, column) {
        //     return row.type == 0 ? '区域角色' : row.type == 1 ? '学校角色' : '其他';
        // },
        selsChange: function (sels) {
                this.sels = sels;
        },
        handleCurrentChange(val) {
            this.formDate.pageNo = val;
            this.init()
        },
        handleSizeChange(val) {
            this.formDate.pageSize = val
            this.init()
            // console.log(`page ${val}`);
        },
        toQuery(){
            this.init()
        },
        async ready(){
            let params = {
                "type": 0
            }
            try {
                let data = await getRoles(params)
                this.roles = data
                
            } catch (e) {
                console.log(e)
            }
            
        },
        add(){
            this.isAdd = true
            this.$refs.form.form = { id:'', userId: '', password: '',mobile:'', userType:0,status: 0,cardId:'',roleIds:[],remark:'',areaCode:'',email:'' },
            this.$refs.form.dialog = true
            this.$refs.form.show = true
        },
        init(){
            this.listLoading = true 
            let params = {...this.formDate}
            userList(params).then(res => { 
                let { list, total } = res
                this.lists = list
                this.total = total 
                this.listLoading = false 
            })
        },
        async handleEdit(index, id) {
            try {
               let Result = await editUser(id)
               this.isAdd = false
               this.$refs.form.dialog = true
               let leiXins = Result.data 
               let leiXin = leiXins.userType.toString()
               leiXins.userType = leiXin 
               this.$refs.form.form = leiXins 
               this.$refs.form.show = false
               this.$refs.form.disabled = false
            } catch (e) {
                console.log(e)
            }
        },
        batchRemove(){
            let ids = this.sels.map(item => item.id);
            this.$confirm('确认删除选中的记录吗?', '提示', {
                        type: 'warning'
                    }).then(() => {
                        this.listLoading = true;
                        userDel(ids).then(res => {
                            this.listLoading = false;
                            this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                            if (this.lists.length == 1 && this.formDate.pageNo != 1) 
                            this.formDate.pageNo--
                            this.init();
                        })

                    }).catch(() => {
            });
        },
        handleDel(index,ID){
            let ids = [];
            ids.unshift(ID)
            this.$confirm('确认删除该记录吗?', '提示', {
                        type: 'warning'
                    }).then(() => {
                        // console.log(ids)
                        this.listLoading = true;
                        userDel(ids).then(res => {
                            this.listLoading = false;
                            this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                            if (this.lists.length == 1 && this.formDate.pageNo != 1) 
                            this.formDate.pageNo--
                            this.init();
                        })

                    }).catch(() => {
            });
        }
        
    }
}

</script>


<style scoped lang="scss">

</style>
